package com.milk.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.milk.system.converter.MerchantsConverter;
import com.milk.system.mapper.MerchantsMapper;
import com.milk.system.model.entity.Merchants;
import com.milk.system.model.form.MerchantsForm;
import com.milk.system.model.query.MerchantsPageQuery;
import com.milk.system.model.vo.MerchantsPageVO;
import com.milk.system.service.MerchantsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 商家信息表服务实现类
 *
 * @author milk
 * @since 2024-05-02
 */
@Service
@RequiredArgsConstructor
public class MerchantsServiceImpl extends ServiceImpl<MerchantsMapper, Merchants> implements MerchantsService {

    private final MerchantsConverter merchantsConverter;

    /**
     * 获取商家信息表分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<MerchantsPageVO>} 商家信息表分页列表
     */
    @Override
    public IPage<MerchantsPageVO> listPagedMerchantss(MerchantsPageQuery queryParams) {

        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        LambdaQueryWrapper<Merchants> wrapper = new LambdaQueryWrapper<>();


        wrapper.like(StrUtil.isNotBlank(queryParams.getMerchantName()), Merchants::getMerchantName, queryParams.getMerchantName())
                .like(StrUtil.isNotBlank(queryParams.getContactName()), Merchants::getContactName, queryParams.getContactName())
                .like(StrUtil.isNotBlank(queryParams.getContactEmail()), Merchants::getContactEmail, queryParams.getContactEmail())
                .like(StrUtil.isNotBlank(queryParams.getContactPhone()), Merchants::getContactPhone, queryParams.getContactPhone())
                .between(queryParams.getStartRegistrationDate() != null && queryParams.getEndRegistrationDate() != null, Merchants::getRegistrationDate, queryParams.getStartRegistrationDate(), queryParams.getEndRegistrationDate())
                .eq(queryParams.getStatus() != null, Merchants::getStatus, queryParams.getStatus())
                .orderByDesc(Merchants::getCreatedTime);

        Page<Merchants> page = this.page(new Page<>(pageNum, pageSize), wrapper);

        return merchantsConverter.bo2PageVo(page);
    }

    /**
     * 获取商家信息表表单数据
     *
     * @param id 商家信息表ID
     * @return
     */
    @Override
    public MerchantsForm getMerchantsFormData(Long id) {
        Merchants entity = this.getById(id);
        return merchantsConverter.entity2Form(entity);
    }

    /**
     * 新增商家信息表
     *
     * @param formData 商家信息表表单对象
     * @return
     */
    @Override
    public boolean saveMerchants(MerchantsForm formData) {
        // 实体转换 form->entity
        Merchants entity = merchantsConverter.form2Entity(formData);
        LocalDateTime dateTime = LocalDateTime.now();
        entity.setCreatedTime(dateTime);
        entity.setDeleted(Boolean.FALSE);
        entity.setUpdatedTime(dateTime);
        entity.setRegistrationDate(dateTime);
        return this.save(entity);
    }

    /**
     * 更新商家信息表
     *
     * @param id       商家信息表ID
     * @param formData 商家信息表表单对象
     * @return
     */
    @Override
    public boolean updateMerchants(Long id, MerchantsForm formData) {
        Merchants entity = merchantsConverter.form2Entity(formData);
        entity.setUpdatedTime(LocalDateTime.now());
        return this.updateById(entity);
    }

    /**
     * 删除商家信息表
     *
     * @param ids 商家信息表ID，多个以英文逗号(,)分割
     * @return true|false
     */
    @Override
    public boolean deleteMerchantss(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的商家信息表数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }


}
